package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	/** Defines a shape in which no point can be placed */
	public class OobShape extends Sprite
	{	
		//public constant
		public static var DEPTH:Number = 30;
		public static const OUTLINE_COLOR:uint = 0x886644;
		public static const BORDER_COLOR:uint = 0xC8C8D0;
		public static const INSIDE_COLOR:uint = 0xE0E0F2;
		//Public variables
		private var unit:Number;
		// Private objects
		private var ishape:Sprite, bshape:Sprite;
		public var points:Array; // stores the points that define this shape
		
		public function OobShape(points:Array)
		{
			// this must be a triangle or greater
			if (points.length >= 3)
			{
				this.points = points;
				redraw();
			} else
			{
				points = null;
			}
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			unit = ShapeProductionTask.unit;
			DEPTH *= unit;
		}
		
		public function redraw():void
		{
			if (this.bshape == null)
			{
				this.bshape = new Sprite();
				bshape.mouseEnabled = false;
				this.addChild(bshape);
			}
			if (this.ishape == null)
			{
				this.ishape = new Sprite();
				ishape.mouseEnabled = false;
				this.addChild(ishape);
			}
			// mins and maxs
			var minx:Number=Number.MAX_VALUE, miny:Number=Number.MAX_VALUE, maxx:Number=Number.MIN_VALUE, maxy:Number=Number.MIN_VALUE;
			var ipoints:Array;
			
			// get min and max, put points on each corner
			for each (var p:Point in this.points)
			{
				if (p.x < minx) minx = p.x;
				if (p.x > maxx) maxx = p.x;
				if (p.y < miny) miny = p.y;
				if (p.y > maxy) maxy = p.y;
			}
			
			
			//draw with respect to max and min
			bshape.graphics.clear();
			bshape.graphics.lineStyle(2, OUTLINE_COLOR);
			
			var itype:String = GradientType.LINEAR;
			var btype:String = GradientType.LINEAR;
			//var bcolors:Array = [0x000088, 0xD8D8EF]; 
			//var icolors:Array =  [0xAAAADF, 0xD0D0EF]; 
			var bcolors:Array = [0xAA8866, 0xCCAA88]; 
			var icolors:Array =  [0x88AA66, 0xAACC88 ]; 
			var alphas:Array = [1, 1]; 
			var bratios:Array = [0, 255]; 
			var iratios:Array = [0, 255]; 
			var spreadMethod:String = SpreadMethod.PAD; 
			var interp:String = InterpolationMethod.LINEAR_RGB; 
			var focalPtRatio:Number = 0; 
			
			var matrix:Matrix = new Matrix(); 
			var boxWidth:Number = maxx - minx; 
			var boxHeight:Number = maxy - miny; 
			var boxRotation:Number = Math.PI/2; 
			var tx:Number = - (maxx - minx)/2; 
			var ty:Number = - (maxy - miny)/2; 
			//trace(minx, maxx, miny, maxy, tx, ty);
			matrix.createGradientBox(boxWidth, boxHeight, boxRotation, tx, ty); 
			//trace(type, bcolors, alphas, ratios, matrix, spreadMethod, interp, focalPtRatio);
			bshape.graphics.beginGradientFill(btype, bcolors, alphas, bratios, matrix, spreadMethod, interp, focalPtRatio);
			bshape.graphics.moveTo(points[points.length-1].x, points[points.length-1].y); 
			
			for each (p in this.points)
			{
				bshape.graphics.lineTo(p.x, p.y); 
			}
			bshape.graphics.endFill();
			
			ishape.graphics.clear();
			ishape.graphics.lineStyle(1, OUTLINE_COLOR);
			ishape.graphics.beginGradientFill(itype, icolors, alphas, iratios, matrix, spreadMethod, interp, focalPtRatio);
			
			var c:Point = MathUtilities.centroid(this.points);
			bshape.graphics.lineStyle(1, OUTLINE_COLOR);
			
			var angToCentroid:Number = MathUtilities.calcAngle(c, points[points.length-1]);
			ishape.graphics.moveTo(points[points.length-1].x+DEPTH*Math.sin(angToCentroid), points[points.length-1].y-DEPTH*Math.cos(angToCentroid)); 
			
			for each (p in this.points)
			{
				angToCentroid = MathUtilities.calcAngle(c, p);
				ishape.graphics.lineTo(p.x+DEPTH*Math.sin(angToCentroid), p.y-DEPTH*Math.cos(angToCentroid)); 
				// draw line on bshape from bpoint to ipoint
				bshape.graphics.moveTo(p.x, p.y);
				bshape.graphics.lineTo(p.x+DEPTH*Math.sin(angToCentroid), p.y-DEPTH*Math.cos(angToCentroid));				
			}
			ishape.graphics.endFill();
		}
	}
}